package com.cat.math;

import javax.imageio.ImageTranscoder;
import java.util.Arrays;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/assign-elements-to-groups-with-constraints/
 * @create 2025/10/17 19:49
 * @since JDK17
 */

public class Solution17 {
    public int[] assignElements(int[] groups, int[] elements) {
        int max = 0;
        for (int num : groups) {
            max = Math.max(max, num);
        }
        int[] f = new int[max + 1];
        Arrays.fill(f, -1);
        for (int x : elements) {
            if (x > max || f[x] >= 0) {
                continue;
            }
            for (int y = x; y <= max; y += x) {
                if (f[y] < 0) {
                    f[y] = x;
                }
            }
        }
        for (int i = 0; i < groups.length; i++) {
            groups[i] = f[groups[i]];
        }

        return groups;
    }
}
